

#include "sqlite.hpp"
#include <assert.h>
#include <vector>

int select_stu_callback(void *arg, int clo_num, char **result, char **erro)
{
    std::vector<std::string> *array = (std::vector<std::string> *)arg;
    array->push_back(result[0]);
    return 0; // 必须有 返回值，返回0表示正常执行，否则abort
}
int main()
{
    SqliteHelper helper("./testdb");
    // 1,创建打开数据库
    helper.open();
    // 2，创建表——学生表 字段：学号，姓名，年龄
    const char *ct = "create table if not exists student(sn int primary key,name varchar(20),age int);";
    assert(helper.exec(ct, nullptr, nullptr));

    // 3,对表进行增，删，查，该
    std::string insert_sql = "insert into student values(1,'张三',18),(2,'李四',20),(3,'王五',30);";
    assert(helper.exec(insert_sql.c_str(), nullptr, nullptr));

    std::string update_sql = "update student set age=22 where sn=1;";
    assert(helper.exec(update_sql.c_str(), nullptr, nullptr));

    std::string delete_sql = "delete from student where sn=3;";
    assert(helper.exec(delete_sql.c_str(), nullptr, nullptr));

    std::string select_sql = "select name from student;";
    std::vector<std::string> arr;
    assert(helper.exec(select_sql.c_str(), select_stu_callback, &arr));
    for (auto str : arr)
        std::cout << str << std::endl;
    helper.close();
    return 0;
}